<template>
  <el-drawer :visible.sync="drawer" direction="rtl" size="50%" :with-header="false" :width="700" append-to-body>
    <div class="drawer_box">
      <div class="drawer_title">
        <div class="drawer_t">
          <div class="drawer_t_l">
            <img class="drawer_t_l_img" src="/static/skin/herodoo/main/platform/home/img/piliangoumai.png" alt="" />
            申请开票
          </div>
          <img class="drawer_t_r" src="/static/skin/herodoo/main/platform/home/img/guanbi.png" alt=""
            @click="drawer = false" />
        </div>
      </div>
      <div class="drawer_content">
        <div class="title">发票信息：</div>
        <div class="content-box">
          <div class="label">订单编号：</div>
          <div class="desc">{{ messageList.PayCode }}</div>
        </div>
        <div class="content-box">
          <div class="label">权益名称：</div>
          <div class="desc name" @click="handleShowDetail">
            {{ messageList.Name }}
          </div>
        </div>
        <div class="content-box">
          <div class="label">订单购买数量：</div>
          <div class="desc">{{ messageList.Quantity }}</div>
        </div>
        <div class="content-box">
          <div class="label">订单支付总金额(元)：</div>
          <div class="desc">{{ messageList.UserPayTotalPrice }}</div>
        </div>
        <div class="content-box">
          <div class="label">卖家：</div>
          <div class="desc">{{ messageList.CompanyIDName }}</div>
        </div>
        <div class="content-box">
          <div class="label">购买时间：</div>
          <div class="desc">{{ messageList.PayTime }}</div>
        </div>
        <div class="content-box">
          <div class="label">开票总金额(元)：</div>
          <div class="desc">
            {{
              (messageList.UserPayTotalPrice * 100 -
                messageList.RefundAmount * 100) /
              100
            }}
          </div>
        </div>
        <br />
        <div class="title">发票信息：</div>
        <div class="content-box">
          <div class="label">抬头类型：</div>
          <div class="desc">
            <el-radio-group v-model="invoiceForm.TypeID" @change="changeTypeID">
              <el-radio label="1">企业</el-radio>
              <el-radio label="2" :disabled="messageList.TradeTypeID == 1">个人</el-radio>
            </el-radio-group>
          </div>
        </div>
        <div class="content-box">
          <div class="label">单位名称：</div>
          <div class="desc">
            <el-input v-model="invoiceForm.CompanyName" placeholder="请输入单位名称" @change="changeCompanyName"></el-input>
          </div>
        </div>
        <div class="content-box" v-if="invoiceForm.TypeID == 1">
          <div class="label">税号：</div>
          <div class="desc">
            <el-input v-model="invoiceForm.TaxCode" placeholder="请输入税号" @change="changeTaxCode"></el-input>
          </div>
        </div>
        <div class="content-box">
          <div class="label">单位地址：</div>
          <div class="desc">
            <el-input v-model="invoiceForm.CompanyAddress" placeholder="请输入单位地址"
              @change="changeCompanyAddress"></el-input>
          </div>
        </div>
        <div class="content-box">
          <div class="label">单位电话：</div>
          <div class="desc">
            <el-input v-model="invoiceForm.Phone" placeholder="请输入单位电话" @change="changePhone"></el-input>
          </div>
        </div>
        <div class="content-box">
          <div class="label">开户银行：</div>
          <div class="desc">
            <el-input v-model="invoiceForm.BankName" placeholder="请输入开户银行" @change="changeBankName"></el-input>
          </div>
        </div>
        <div class="content-box">
          <div class="label">银行账号：</div>
          <div class="desc">
            <el-input v-model="invoiceForm.BankAccount" placeholder="请输入银行账号" @change="changeBankAccount"></el-input>
          </div>
        </div>
      </div>
      <div class="drawer_footer">
        <button class="footer-btn" @click="handleSubmit">提交</button>
      </div>
    </div>
    <!-- 选择权益弹窗 -->
    <add-equity :octopus="octopus" @invoiceList="invoiceList" ref="AddEquity" :checkList="[1]"></add-equity>
    <!-- 公司补充发票信息弹窗 -->
    <update-company-info :octopus="octopus" ref="UpdateCompanyInfo"></update-company-info>
    <!-- 查看权益弹窗 -->
    <rights-detail :octopus="octopus" ref="rightsDetail"></rights-detail>
  </el-drawer>
</template>

<script>
import AddEquity from "./AddEquity.vue";
import UpdateCompanyInfo from "./UpdateCompanyInfo.vue";
import rightsDetail from "./rightsDetail.vue";
export default {
  components: { AddEquity, UpdateCompanyInfo, rightsDetail },
  props: {
    octopus: {
      type: Object,
      default: {}
    },
    messageList: {
      type: Object,
      default: {}
    }
  },
  data() {
    return {
      drawer: false,
      invoiceForm: {
        CompanyName: "", // 单位名称
        TaxCode: "", // 购方税号
        CompanyAddress: "", // 单位地址
        Phone: "", // 开票人联系电话
        BankName: "", // 开户银行
        BankAccount: "", // 银行账户
        ID: "",
        Mail: "", // 开票人 邮箱
        TypeID: "1" // 抬头类型
      },
      rules: {
        TypeID: { required: true, message: "请选择开票类型" },
        CompanyName: { required: true, message: "请输入单位名称" },
        TaxCode: {
          required: true,
          message: "请输入税号",
          reg: () => {
            if (this.invoiceForm.TypeID == 2) {
              return (
                this.invoiceForm.TaxCode != null &&
                this.invoiceForm.TaxCode.trim() != ""
              );
            }
            return true;
          }
        },
        CompanyAddress: { required: true, message: "请输入单位地址" },
        Phone: { required: true, message: "请输入联系电话" },
        BankName: { required: true, message: "请输入开户银行" },
        BankAccount: { required: true, message: "请输入银行账号" }
      },
      flagNum: 0 // 发票信息是否改动
    };
  },
  mounted() { },
  created() {


  },
  watch: {
    drawer(v) {
      if (v) {
        this.getInvoiceForm();
      }
    }
  },
  methods: {
    drawerOp() {
      this.drawer = true;
    },
    // 获取发票信息
    getInvoiceForm() {
      this.octopus.request.api.post(
        "/Octopus/Engine/List",
        {
          id: 1296,
          cellData: [
            { name: "BankAccount" }, // 银行账户
            { name: "BankName" }, // 开户银行
            { name: "CompanyAddress" }, // 单位地址
            { name: "CompanyName" }, // 单位名称
            { name: "ID" }, // ID
            { name: "Mail" }, // 开票人 邮箱
            { name: "Phone" }, // 开票人联系电话
            { name: "TaxCode" }, // 购方税号
            { name: "TypeID" } // 抬头类型
          ],
          paramData: {
            CreateUserID: this.octopus.passport.id
          }
        },
        data => {
          if (data.status.code == 200) {
            data.content.queryData.length &&
              (this.invoiceForm = data.content.queryData[0]);
          } else {
            this.octopus.tips.error(data);
          }
        }
      );
    },
    // 更新发票信息
    updateInvoiceForm(type, id) {
      this.octopus.request.api.post(
        "/Octopus/Engine/Item",
        {
          id: 1285,
          value: id,
          type: type,
          cellData: [
            { name: "BankAccount", defaultValue: this.invoiceForm.BankAccount }, // 银行账户
            { name: "BankName", defaultValue: this.invoiceForm.BankName }, // 开户银行
            {
              name: "CompanyAddress",
              defaultValue: this.invoiceForm.CompanyAddress
            }, // 单位地址
            { name: "CompanyName", defaultValue: this.invoiceForm.CompanyName }, // 单位名称
            { name: "Mail", defaultValue: this.invoiceForm.Mail }, // 开票人 邮箱
            { name: "Phone", defaultValue: this.invoiceForm.Phone }, // 开票人联系电话
            {
              name: "TaxCode",
              defaultValue:
                this.invoiceForm.TypeID == 1 ? this.invoiceForm.TaxCode : ""
            }, // 购方税号
            { name: "TypeID", defaultValue: this.invoiceForm.TypeID }, // 抬头类型
            {
              name: "CreateTime",
              defaultValue:
                new Date().toJSON().substring(0, 10) +
                " " +
                new Date().toTimeString().substring(0, 8)
            }, // CreateTime
            { name: "CreateUserID", defaultValue: this.octopus.passport.id } // CreateUserID
          ]
        },
        data => {
          if (data.status.code == 200) {
          } else {
            this.octopus.tips.error(data);
          }
        }
      );
    },
    // 提交
    handleSubmit() {

      if (!this.octopus.tools.verify(this.invoiceForm, this.rules)) {
        return;
      }
      if (this.flagNum) {
        if (this.invoiceForm.ID) {
          this.updateInvoiceForm("UPDATE", this.invoiceForm.ID);
        } else {
          this.updateInvoiceForm("CREATE");
        }
      }
      this.$confirm("该操作只允许操作一次，是否确认开票?", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        // if(this.messageList.InvoiceID) {
        // this.updatePurchaseInvoice("UPDATE", this.messageList.InvoiceID)
        // } else {
        if (this.messageList.StatusID == 4 || this.messageList.StatusID == 3) {
          this.updatePurchaseInvoice("UPDATE", this.messageList.InvoiceID, 1);
        }
        this.updatePurchaseInvoice("CREATE");

        // }
      });
    },
    // 更新申请开票表
    updatePurchaseInvoice(type, id, status = 0) {
      let cellData = [];

      if (type == "CREATE") {
        cellData = [
          { name: "PurchaseID", defaultValue: this.messageList.ID }, // 订单ID
          { name: "CompanyID", defaultValue: this.messageList.CompanyID }, // 公司ID
          { name: "TypeID", defaultValue: this.invoiceForm.TypeID }, // 发票抬头类型（1企业单位  2个人/非企业单位）
          // {name: "FlagID"},	// 发票类型，1:餐饮；2:服务；3:鲜花...
          { name: "Name", defaultValue: this.messageList.Name }, // 开票的权益名称
          { name: "CompanyName", defaultValue: this.invoiceForm.CompanyName }, // 公司
          {
            name: "TaxCode",
            defaultValue:
              this.invoiceForm.TypeID == 1 ? this.invoiceForm.TaxCode : ""
          }, // 发票税号
          {
            name: "CompanyAddress",
            defaultValue: this.invoiceForm.CompanyAddress
          }, // 公司地址
          { name: "Phone", defaultValue: this.invoiceForm.Phone }, // Phone
          { name: "BankName", defaultValue: this.invoiceForm.BankName }, // 开户银行
          { name: "BankAccount", defaultValue: this.invoiceForm.BankAccount }, // 银行账户
          // {name: "Remarks"},	// Remarks
          {
            name: "Amount",
            defaultValue:
              (this.messageList.UserPayTotalPrice * 100 -
                this.messageList.RefundAmount * 100) /
              100
          }, // 开票金额
          { name: "IsDelete", defaultValue: status }, // 是否重开 （0 正常，1 重开）
          { name: "IsVat" }, // 是否增值税发票 (0普通发票, 1增值发票)
          { name: "CreateUserID", defaultValue: this.octopus.passport.id }, // 申请人
          {
            name: "CreateTime",
            defaultValue:
              new Date().toJSON().substring(0, 10) +
              " " +
              new Date().toTimeString().substring(0, 8)
          } // 申请时间
        ];
      } else {
        cellData = [
          {
            name: "ReopenTime",
            defaultValue:
              new Date().toJSON().substring(0, 10) +
              " " +
              new Date().toTimeString().substring(0, 8)
          }, //
          { name: "IsDelete", defaultValue: status } // 是否重开 （0 正常，1 重开）
        ];
      }

      this.octopus.request.api.post(
        "/Octopus/Engine/Item",
        {
          id: 1211,
          type,
          value: id,
          cellData: cellData
        },
        data => {
          if (data.status.code == 200) {
            if (type == 'UPDATE') {
              return
            }
            this.octopus.tips.success("发票申请已提交");
            this.drawer = false;
            this.$emit("refresh");
            // 获取用户组
            this.$utils.getGroupInfo(this, '超级管理员,平台财务,企业高级管理员,企业财务', this.messageList.CompanyID).then((res) => {
              console.log('发送消息的用户组：', res);
              // 发送消息 传值说明 模版ID ，消息类型 ，消息里的字段 ，创建用户ID，接收公司ID，接收群组ID
              res.length > 0 && this.$utils.sendMessageMulti(11, 3, {PurchaserName: this.messageList.MerchantIDName }, this.octopus.passport.id, this.messageList.CompanyID, res, this)
            });
          } else {
            this.octopus.tips.error(data);
          }
        }
      );
    },

    // 打开选择开票权益弹窗
    selectEquity() {
      this.$refs.AddEquity.drawerOp();
    },

    // 选择的开票权益
    invoiceList(list) { },
    // 权益详情弹窗
    handleShowDetail() {
      this.$emit("drawerUpDetail");
    },
    changeTypeID(e) {
      this.flagNum += 1;
    },
    changeCompanyName(e) {
      this.flagNum += 1;
    },
    changeTaxCode(e) {
      this.flagNum += 1;
    },
    changeCompanyAddress(e) {
      this.flagNum += 1;
    },
    changePhone(e) {
      this.flagNum += 1;
    },
    changeBankName(e) {
      this.flagNum += 1;
    },
    changeBankAccount(e) {
      this.flagNum += 1;
    },
    claneC() {
      this.octopus.request.api.post(
        "/Octopus/Engine/Item",
        {
          id: 1211,
          type: "UPDATE",
          value: this.messageList.InvoiceID,
          cellData: [
            {
              name: "ReopenTime",
              defaultValue:
                new Date().toJSON().substring(0, 10) +
                " " +
                new Date().toTimeString().substring(0, 8)
            }, // 是否重开 （0 正常，1 重开）
            { name: "IsDelete", defaultValue: 1 } // 是否重开 （0 正常，1 重开）
          ]
        },
        data => {
          if (data.status.code == 200) {
            this.octopus.tips.success("发票申请已撤销");
            this.drawer = false;
            this.$emit("refresh");
          } else {
            this.octopus.tips.error(data);
          }
        }
      );
    }
  }
};
</script>
<style scoped>
.drawer_box {
  padding: 3rem 0.8rem 3rem 0;
  height: calc(100% - 5rem);
  display: flex;
  flex-direction: column;
}

.drawer_title {
  box-shadow: 0px 5px 5px rgb(230 228 228 / 30%);
  padding-bottom: 1rem;
}

.drawer_t {
  padding: 0 2.8rem;
  display: flex;
  justify-content: space-between;
}

.drawer_t_l {
  display: flex;
  align-items: center;
  font-size: 2rem;
  font-weight: bold;
}

.drawer_t_l_img {
  width: 2.8rem;
  margin-right: 1rem;
}

.drawer_t_r {
  width: 3rem;
  height: 3rem;
  cursor: pointer;
}

.button_box {
  margin-top: 1rem;
  padding: 0 3rem;
  display: flex;
}

.drawer_content {
  overflow-y: auto;
  margin-top: 1.5rem;
  height: calc(100vh - 11rem);
  padding: 0 3rem;
  width: 100%;
  padding-bottom: 1rem;
}

.drawer_content .title {
  font-size: 1.8rem;
  font-weight: bold;
}

.drawer_content .driver-title {
  margin-top: 1em;
}

.full-line {
  margin: 2rem auto;
  width: 95%;
  height: 1px;
  background-color: #f78c51;
}

.content-box {
  display: flex;
  align-items: center;
  margin: 2rem 0 2rem 2em;
}

.mt-10 {
  margin-top: 10rem;
}

.content-box .label {
  width: 25%;
  font-size: 1.6rem;
}

.content-box .desc {
  flex: 1;
  font-size: 1.6rem;
}

.content-box .desc .desc-img {
  width: 14rem;
  height: 14rem;
  border-radius: 1rem;
  display: inline-block;
  margin-right: 1rem;
  background-color: aqua;
}

.select-equity {
  height: 4rem;
  line-height: 4rem;
  background-color: #f78c51;
  color: #fff;
  margin: 0 1rem;
  padding: 0 3rem;
  border-radius: 0.5rem;
  cursor: pointer;
  border: none;
  text-align: right;
  position: relative;
}

.select-equity::before,
.select-equity::after {
  position: absolute;
  content: "";
  width: 10px;
  height: 2px;
  top: 50%;
  transform: translateY(-50%);
  z-index: 1;
  background-color: #fff;
  left: calc(3rem - 15px);
}

.select-equity::after {
  transform: translateY(-50%) rotate(90deg);
}

.drawer_footer {
  width: 100%;
  display: flex;
  justify-content: flex-end;
  margin-right: 30px;
}

.drawer_footer .footer-btn {
  line-height: 4rem;
  background-color: #f78c51;
  color: #fff;
  margin: 0 1rem;
  padding: 0 3rem;
  border-radius: 0.5rem;
  cursor: pointer;
  border: none;
  font-size: 1.6rem;
}
</style>
